﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class GetVoteActivityItemList : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            VoteActivityItemOutParam result = new VoteActivityItemOutParam();
            VoteActivityItemInParam param = new VoteActivityItemInParam();
            string logMsg = "";
            bool isFlag = true;

            try
            {
                param = inParam as VoteActivityItemInParam;
                if (string.IsNullOrEmpty(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户ID不能为空";

                    isFlag = false;
                }

                if (CommonUtils.IsDisableUsers(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户状态有问题，请联系管理员";

                    isFlag = false;
                }
                if (string.IsNullOrEmpty(param.ActivityID))
                {
                    result.code = "0";
                    result.desc = "机关评议id不能为空";

                    isFlag = false;
                }

                if (param.page_no * param.page_count <= 0)
                {
                    result.code = "0";
                    result.desc = "当前页,以及页记录条数必须为大于0的正整数";

                    isFlag = false;
                }

                int haveVoteNum = 0;
                int notVoteNum = 0;
                if (isFlag)
                {
                    DataSet dsState = InterfaceHelper.GetVoteItemByID(param.user_id, param.ActivityID, param.State);
                    if (dsState != null && dsState.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow drr in dsState.Tables[0].Rows)
                        {
                            if (Convert.ToString(drr["VoteState"]) == "1")
                                haveVoteNum++;
                        }

                        notVoteNum = dsState.Tables[0].Rows.Count - haveVoteNum;
                    }

                    //获取被评议对象列表 
                    DataSet ds = InterfaceHelper.GetVoteItemByPage(param.user_id, param.ActivityID, param.VoteState, param.Key, param.page_no, param.page_count, param.State);
                    if (ds != null && ds.Tables.Count == 2)
                    {
                        result.data.total_pages = CommonUtils.GetTotalPages(Convert.ToInt32(ds.Tables[0].Rows[0][0]), param.page_count);
                        VoteItemEx item = new VoteItemEx();
                        foreach (DataRow drr in ds.Tables[1].Rows)
                        {
                            item = new VoteItemEx();
                            item.BranchName = Convert.ToString(drr["BranchName"]);
                            item.ItemID = Convert.ToString(drr["ItemID"]);
                            item.Title = Convert.ToString(drr["Title"]);
                            item.VoteDesc = Convert.ToString(drr["VoteDesc"] ?? "");
                            item.VoteResult = Convert.ToString(drr["VoteResult"]);
                            item.VoteState = Convert.ToString(drr["VoteState"]);
                            //if (param.VoteState == "1" && item.VoteState != "0")
                            //    continue;
                            //else if (param.VoteState == "2" && item.VoteState != "1")
                            //    continue;

                            result.data.list.Add(item);
                        }
                        result.HaveVoteNum = haveVoteNum.ToString();
                        result.NotVoteNum = notVoteNum.ToString();
                    }
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-GetVoteActivityItemList", SerializeHelper.JsonSerializer<VoteActivityItemInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<VoteActivityItemInParam, VoteActivityItemOutParam>();
            else
                SafeProcessRequest<VoteActivityItemInParam, VoteActivityItemOutParam>();
        }
    }

    #region 请求参数
    public class VoteActivityItemInParam
    {
        /// <summary>
        /// 必填， 	用户ＩＤ
        /// </summary>
        public string user_id
        { get; set; }

        /// <summary>
        /// 必填， 机关评议id
        /// </summary>
        public string ActivityID
        { get; set; }

        /// <summary>
        ///	被评议对象关键字
        /// </summary>
        public string Key
        { get; set; }

        /// <summary>
        /// 必填， 评议状态 0：全部；1：未评议；2：已评议
        /// </summary>
        public string VoteState
        { get; set; }

        /// <summary>
        /// 必填 当前页
        /// </summary>
        public int page_no
        { get; set; }

        /// <summary>
        /// 必填，每页记录条数
        /// </summary>
        public int page_count
        { get; set; }

        /// <summary>
        /// 类型（分组）
        /// </summary>
        public string State
        { get; set; }
    }
    #endregion

    #region 结果参数
    public class VoteActivityItemOutParam
    {
        public VoteActivityItemOutParam()
        {
            code = "1";
            desc = "操作成功";
            data = new VoteItemExList();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        /// 被评议对象列表
        /// </summary>
        public VoteItemExList data
        { get; set; }

        /// <summary>
        ///	已评议数量
        /// </summary>
        public string HaveVoteNum
        { get; set; }

        /// <summary>
        ///	未评议数量
        /// </summary>
        public string NotVoteNum
        { get; set; }
    }

    public class VoteItemExList
    {
        public VoteItemExList()
        {
            total_pages = 0;
            list = new List<VoteItemEx>();
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int total_pages
        { get; set; }

        public List<VoteItemEx> list { get; set; }
    }

    /// <summary>
    /// 被评议对象
    /// </summary>
    public class VoteItemEx
    {
        /// <summary>
        ///	被评议对象ID		
        /// </summary>
        public string ItemID
        { get; set; }

        /// <summary>
        ///	被评议标题		
        /// </summary>
        public string Title
        { get; set; }

        /// <summary>
        ///	被评议机关名称		
        /// </summary>
        public string BranchName
        { get; set; }

        /// <summary>
        ///	评议状态 0.未评议；1.已评议	 	
        /// </summary>
        public string VoteState
        { get; set; }

        /// <summary>
        ///	评议结果 如果评议状态为1，则返回相应的评议结果。当前的评议结果包括满意、基本满意、不满意、不了解		
        /// </summary>
        public string VoteResult
        { get; set; }

        /// <summary>
        ///	评议结果描述 如果评议结果是不满意，需要返回结果描述		 
        /// </summary>
        public string VoteDesc
        { get; set; }


    }
    #endregion
}